<?php

/**
 * 缩略图
 */

namespace App\Http\Controllers\Admin;

use App\Common\Utill\Utills;
use App\Http\Controllers\Controller;
use App\Models\Banners;
use App\Service\BannersService;
use App\Service\WebsitesService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;

class BannersController extends Controller
{
    public function index(Request $request)
    {
        //获取网站数据
        $webSiteData = WebsitesService::getWebsiteData();
        return view('admin.banners.index')
            ->with('websiteData', $webSiteData);
    }

    /**
     * 分类列表数据
     * @return void
     */
    public function listData(Request $request)
    {
        $num = $request->input('limit', self::ADMIN_PAGE_NUM);
        $server = new BannersService();
        $result = $server->getListData($request, $num);
        return Utills::resultData($result);
    }

    /**
     * 添加展示
     * @return void
     */
    public function createForm()
    {
        $websiteData = WebsitesService::getWebsiteData(1);
        return view('admin.banners.create')->with('websiteData', $websiteData);
    }

    /**
     * 添加报存
     * @param Request $request
     * @return void
     */
    public function store(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'websiteId' => 'required',
            'thumbs' => 'required',
        ], [
            'websiteId.required' => '请选择网站',
            'thumbs.required' => '请上传缩略图',
        ]);
        if ($validator->fails()) {
            $result = ['code' => 1, 'msg' => $validator->errors()->first()];
            return response()->json($result);
        }
        $websiteId = $request->input('websiteId', "");
        $jump_url = $request->input('jump_url', "");
        $sort = $request->input('sort', 1);
        $thumbs = $request->input('thumbs', "");
        $titles = $request->input('titles', "");
        $describes = $request->input('describes', "");
        $server = new BannersService();
        $result = $server->store($websiteId, $jump_url, $sort, $thumbs,$titles,$describes);
        return Utills::resultData($result);
    }

    public function editForm(Request $request)
    {
        $id = $request->input('id', 0);
        $data = Banners::where(['id' => $id])->first();
        $websiteData = WebsitesService::getWebsiteData();
        return view('admin.banners.edit')->with('data', $data)->with('websiteData', $websiteData);
    }

    public function update(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'websiteId' => 'required',
            'thumbs' => 'required',
        ], [
            'websiteId.required' => '请选择网站',
            'thumbs.required' => '请上传缩略图',
        ]);
        if ($validator->fails()) {
            $result = ['code' => 1, 'msg' => $validator->errors()->first()];
            return response()->json($result);
        }
        $id = $request->input('id', 0);
        $websiteId = $request->input('websiteId', "");
        $jump_url = $request->input('jump_url', "");
        $sort = $request->input('sort', 1);
        $thumbs = $request->input('thumbs', "");
        $titles = $request->input('titles', "");
        $describes = $request->input('describes', "");
        $server = new BannersService();
        $result = $server->update($id,$websiteId, $jump_url, $sort, $thumbs,$titles,$describes);
        return Utills::resultData($result);
    }

    /**
     * 状态修改
     * @return void
     */
    public function status(Request $request)
    {
        $validator = \Validator::make($request->all(), [
            'id' => 'required',
        ], [
            'id.required' => 'id不能为空',
        ]);
        if ($validator->fails()) {
            $result = ['code' => 404, 'msg' => $validator->errors()->first()];
            return response()->json($result);
        }
        $id = $request->input('id');
        $server = new BannersService();
        $result = $server->status($id);
        return response()->json($result);
    }

    /*
    * 删除
    */
    public function delete(Request $request)
    {
        if (!$request->input('id')) {
            return $this->error('删除失败');
        }
        $id = $request->input('id');
        $server = new BannersService();
        $result = $server->delOne($id);
        return Utills::resultData($result);
    }

    /*
     *全部删除
     */
    public function deleteAll(Request $request)
    {
        $idArr = $request->input('idList', "");
        $server = new BannersService();
        $result = $server->delAll($idArr);
        return Utills::resultData($result);
    }

}
